# 定义棋盘大小
BOARD_SIZE = 8

# 初始化棋盘
def initialize_board():
    board = [[' ' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
    # 初始化白方棋子
    board[0] = ['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']
    board[1] = ['P'] * BOARD_SIZE
    # 初始化黑方棋子
    board[6] = ['p'] * BOARD_SIZE
    board[7] = ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r']
    return board

# 打印棋盘
def print_board(board):
    print("  a b c d e f g h")
    for i in range(BOARD_SIZE):
        print(f"{8 - i}", end=" ")
        for j in range(BOARD_SIZE):
            print(board[i][j], end=" ")
        print(8 - i)
    print("  a b c d e f g h")

# 检查坐标是否在棋盘内
def is_valid_coordinate(x, y):
    return 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE

# 简单的移动规则检查（这里只是简单示例，未涵盖所有规则）
def is_valid_move(board, from_x, from_y, to_x, to_y, current_player):
    piece = board[from_x][from_y]
    if piece == ' ':
        return False
    if (current_player == 'white' and piece.islower()) or (current_player == 'black' and piece.isupper()):
        return False
    if from_x == to_x and from_y == to_y:
        return False
    if not is_valid_coordinate(to_x, to_y):
        return False
    # 简单示例：兵的移动规则
    if piece.upper() == 'P':
        if current_player == 'white':
            if from_x - to_x == 1 and from_y == to_y and board[to_x][to_y] == ' ':
                return True
            elif from_x - to_x == 1 and abs(from_y - to_y) == 1 and board[to_x][to_y].islower():
                return True
        else:
            if to_x - from_x == 1 and from_y == to_y and board[to_x][to_y] == ' ':
                return True
            elif to_x - from_x == 1 and abs(from_y - to_y) == 1 and board[to_x][to_y].isupper():
                return True
    return True

# 移动棋子
def move_piece(board, from_x, from_y, to_x, to_y):
    piece = board[from_x][from_y]
    board[from_x][from_y] = ' '
    board[to_x][to_y] = piece
    return board

# 主游戏循环
def main():
    board = initialize_board()
    current_player = 'white'
    while True:
        print_board(board)
        print(f"Current player: {current_player}")
        try:
            from_pos = input("Enter the position of the piece to move (e.g., a2): ")
            to_pos = input("Enter the destination position (e.g., a3): ")
            from_x = 8 - int(from_pos[1])
            from_y = ord(from_pos[0]) - ord('a')
            to_x = 8 - int(to_pos[1])
            to_y = ord(to_pos[0]) - ord('a')
            if is_valid_move(board, from_x, from_y, to_x, to_y, current_player):
                board = move_piece(board, from_x, from_y, to_x, to_y)
                current_player = 'black' if current_player == 'white' else 'white'
            else:
                print("Invalid move. Please try again.")
        except (ValueError, IndexError, TypeError):
            print("Invalid input. Please enter valid positions.")

if __name__ == "__main__":
    main()
